package com.lujianfei.filesend.ui.tab.adapter

import androidx.databinding.BindingAdapter
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableField
import androidx.databinding.ObservableInt
import androidx.databinding.ObservableLong
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lujianfei.filesend.R
import com.lujianfei.filesend.base.BaseBindingAdapter
import com.lujianfei.filesend.databinding.AdapterTransferListItemBinding

/**
 * 文件传输列表适配器
 */
class TransferListAdapter(items: ObservableArrayList<TransferListAdapterModel>) :
    BaseBindingAdapter<TransferListAdapterModel, AdapterTransferListItemBinding>(items) {

    var onItemClick:((TransferListAdapterModel)->Unit) ?= null

    override fun getLayoutResId() = R.layout.adapter_transfer_list_item

    override fun onBindItem(
        binding: AdapterTransferListItemBinding?,
        bean: TransferListAdapterModel,
        position: Int
    ) {
        binding?.root?.setOnClickListener {
        }        
        binding?.model = bean
    }
}

@BindingAdapter(value = ["TransferListAdapter_bindlist",
    "TransferListAdapter_onItemClick"], requireAll = false)
fun TransferListAdapter_binding(
    view: RecyclerView,
    list: ObservableArrayList<TransferListAdapterModel>,
    onItemClick:((TransferListAdapterModel)->Unit) ?= null,
) {
    if (view.adapter != null) {
        return
    }
    val adapter = TransferListAdapter(list)
    adapter.onItemClick = onItemClick
    view.adapter = adapter
    view.layoutManager = LinearLayoutManager(view.context)
}

data class TransferListAdapterModel(
    val filename: String,
    val progress:ObservableInt,
    val max:ObservableInt
)
